package com.litchi.activity;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

import com.google.zxing.common.StringUtils;
import com.lib.uil.ToastUtils;
import com.litchi.R;
import com.litchi.bean.Address;
import com.litchi.utils.Constants;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class AddressDetailActivity extends Activity implements OnClickListener {

	private EditText nameEd, phoneEd, provinceEd, addressEd;
	private Button okBtn;
	private Address mAddressBean;
	private ImageView backIv;
	private Context mContext;
	private String userKey;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_address_manager);
		Bmob.initialize(this, "881230b0542b0d1540a1b9f77c01804a"); // 初始化 Bmob SDK

		mContext = this;
		initView();
		initData();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	public void initData() {
		mAddressBean = (Address) getIntent().getSerializableExtra(
				Constants.ADDRESS_KEY);
		userKey=getIntent().getStringExtra(Constants.USER_KEY);
		if (null != mAddressBean) {
			nameEd.setText(mAddressBean.getName());
			phoneEd.setText(mAddressBean.getPhone());
			provinceEd.setText(mAddressBean.getProvince());
			addressEd.setText(mAddressBean.getAddress());

		}

	}

	public void initView() {
		backIv = (ImageView) findViewById(R.id.img_back);
		backIv.setOnClickListener(this);
		nameEd = (EditText) findViewById(R.id.edit_name);
		phoneEd = (EditText) findViewById(R.id.edit_phone);
		provinceEd = (EditText) findViewById(R.id.edit_province);
		addressEd = (EditText) findViewById(R.id.edit_address);
		okBtn = (Button) findViewById(R.id.btn_ok_address);
		okBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.img_back:// back main activity
			finish();
			break;
		case R.id.btn_ok_address:
			saveAddress();
			break;
		default:
			break;
		}

	}

	public void saveAddress() {
		String name = nameEd.getText().toString();
		String phone = phoneEd.getText().toString();
		String province = provinceEd.getText().toString();
		String address = addressEd.getText().toString();

		if (null != mAddressBean) {
			if (!TextUtils.isEmpty(name.trim()) && !TextUtils.isEmpty(phone.trim())
					&& !TextUtils.isEmpty(province.trim())
					&& !TextUtils.isEmpty(address.trim())) {
				mAddressBean.setUserId(userKey);
				mAddressBean.setName(name);
				mAddressBean.setPhone(phone);
				mAddressBean.setProvince(province);
				mAddressBean.setAddress(address);
				mAddressBean.setTableName("Address");
				mAddressBean.update(mContext, new UpdateListener(){

					@Override
					public void onFailure(int arg0, String arg1) {
						// TODO Auto-generated method stub
						ToastUtils.showToast(mContext, "修改地址失败" + arg1);

					}

					@Override
					public void onSuccess() {
						ToastUtils.showToast(mContext, "修改地址成功");
						finish();
					}
					
				});
			} else {
				ToastUtils.showToast(mContext, "请检查是否有未填项");

			}

		} else {
			if (!TextUtils.isEmpty(name.trim()) && !TextUtils.isEmpty(phone.trim())
					&& !TextUtils.isEmpty(province.trim())
					&& !TextUtils.isEmpty(address.trim())) {
				Address addressBean = new Address(userKey,province, address,
						phone, name);
				addressBean.setTableName("Address");
				addressBean.save(mContext, new SaveListener() {

					@Override
					public void onSuccess() {
						ToastUtils.showToast(mContext, "添加地址成功");
						finish();

					}

					@Override
					public void onFailure(int arg0, String arg1) {
						// TODO Auto-generated method stub
						ToastUtils.showToast(mContext, "添加地址失败" + arg1);

					}
				});
			} else {
				ToastUtils.showToast(mContext, "请检查是否有未填项");
			}
		}
	}

}
